using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Linq;
using System.Collections.Generic;



namespace msBsc.AppLogic.Cfg
{

    public class gtGridCRecord
    {
        public gtGridCRecord(string s)
        {
            jsonText = s;
            ReadJson();
        }
        public string jsonText
        {
            get;
            set;
        }
        public string recordType
        {
            get;
            set;
        }
        public string exportType
        {
            get;
            set;
        }
        public string exportFileName
        {
            get;
            set;
        }
        public string action
        {
            get;
            set;
        }
        public string custJSPARAM
        {
            get;
            set;
        }
        public string custJSPOST
        {
            get;
            set;
        }
        public bool remotePaging
        {
            get;
            set;
        }
        public ColumnInfo[] columnInfo
        {
            get;
            set;
        }
        public SortInfo[] sortInfo
        {
            get;
            set;
        }
        public FilterInfo[] filterInfo
        {
            get;
            set;
        }
        public msBsc.AppLogic.Cfg.SynDictionary parameters
        {
            get;
            set;
        }
        public PageInfo pageInfo
        {
            get;
            set;
        }
        public msBsc.AppLogic.Cfg.SynDictionary[] insertedRecords
        {
            get;
            set;
        }
        public msBsc.AppLogic.Cfg.SynDictionary[] updatedRecords
        {
            get;
            set;
        }
        public msBsc.AppLogic.Cfg.SynDictionary[] deletedRecords
        {
            get;
            set;
        }
        private string GetStringFromValue(object value)
        {
            string strRet = "";
            if (value == null)
            {
            }
            else if (value.GetType().Name == "String")
            {
                strRet = (string)value;
            }
            else if (value.GetType().Name == "Boolean")
            {
                strRet = ((bool)value).ToString();
            }
            return strRet;
        }

        public string PageInfoJsonString
        {
            get
            {
                return "pageInfo:{\"pageSize\":"+pageInfo.PageSize.ToString()
                    +",\"pageNum\":"+pageInfo.PageNum.ToString()
                    +",\"totalRowNum\":"+pageInfo.TotalRowNum.ToString()
                    +",\"totalPageNum\":"+pageInfo.TotalPageNum.ToString()
                    +",\"startRowNum\":"+pageInfo.StartRowNum.ToString()
                    +",\"endRowNum\":"+pageInfo.EndRowNum.ToString()+"}";
            }
        }

        public void AddFilterInfo(FilterInfo _oFilterInfo)
        {
            if (this.filterInfo == null)
            {
                this.filterInfo = new FilterInfo[0];
            }
            var aaaa = this.filterInfo.Union(new FilterInfo[1] { _oFilterInfo });

            this.filterInfo = aaaa.ToArray();
        }

        public void ReadJson()
        {
            if (!string.IsNullOrEmpty(jsonText))
            {
                JavaScriptSerializer s = new JavaScriptSerializer();

                Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonText);

                foreach (KeyValuePair<string, object> sss in JsonData)
                {
                    if (sss.Key.ToLower() == "recordType".ToLower())
                    {
                        recordType = GetStringFromValue(sss.Value);
                    }
                    if (sss.Key.ToUpper() == "JSPARAM".ToUpper())
                    {
                        custJSPARAM = GetStringFromValue(sss.Value);
                    }
                    if (sss.Key.ToUpper() == "JSPOST".ToUpper())
                    {
                        custJSPOST = GetStringFromValue(sss.Value);
                    }

                    if (sss.Key.ToLower() == "pageInfo".ToLower())
                    {
                        pageInfo = new PageInfo();
                        Dictionary<string, object> JsonData2 = (Dictionary<string, object>)sss.Value;
                        string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                        //JsonData2.Keys.CopyTo(arrKey, 0);
                        for (int i = 0; i < arrKey.Length; i++)
                        { 
                            if (arrKey[i].ToLower() == "EndRowNum".ToLower())
                            {
                                pageInfo.EndRowNum = (int)JsonData2[arrKey[i]];
                                if (pageInfo.EndRowNum == -1) pageInfo.EndRowNum = 0;
                            }
                            if (arrKey[i].ToLower() == "PageNum".ToLower())
                            {
                                pageInfo.PageNum = (int)JsonData2[arrKey[i]];
                            }
                            if (arrKey[i].ToLower() == "PageSize".ToLower())
                            {
                                pageInfo.PageSize = (int)JsonData2[arrKey[i]];
                            }
                            if (arrKey[i].ToLower() == "StartRowNum".ToLower())
                            {
                                pageInfo.StartRowNum = (int)JsonData2[arrKey[i]];
                            }
                            if (arrKey[i].ToLower() == "TotalPageNum".ToLower())
                            {
                                pageInfo.TotalPageNum = (int)JsonData2[arrKey[i]];
                            }
                            if (arrKey[i].ToLower() == "TotalRowNum".ToLower())
                            {
                                pageInfo.TotalRowNum = (int)JsonData2[arrKey[i]];
                                if (pageInfo.TotalRowNum == -1) pageInfo.TotalRowNum = 0;
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "columnInfo".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        columnInfo = new ColumnInfo[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                columnInfo[k] = new ColumnInfo();
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    if (arrKey[i].ToLower() == "Exportable".ToLower())
                                    {
                                        columnInfo[k].Exportable = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "FieldIndex".ToLower())
                                    {
                                        columnInfo[k].FieldIndex = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "FieldName".ToLower())
                                    {
                                        columnInfo[k].FieldName = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "Header".ToLower())
                                    {
                                        columnInfo[k].Header = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "Hidden".ToLower())
                                    {
                                        columnInfo[k].Hidden = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "Id".ToLower())
                                    {
                                        columnInfo[k].Id = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "Printable".ToLower())
                                    {
                                        columnInfo[k].Printable = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "SortOrder".ToLower())
                                    {
                                        columnInfo[k].SortOrder = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                }
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "sortInfo".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        sortInfo = new SortInfo[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                sortInfo[k] = new SortInfo();
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    if (arrKey[i].ToLower() == "ColumnId".ToLower())
                                    {
                                        sortInfo[k].ColumnId = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "FieldName".ToLower())
                                    {
                                        sortInfo[k].FieldName = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "sortOrder".ToLower())
                                    {
                                        sortInfo[k].sortOrder = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "getSortValue".ToLower())
                                    {
                                        sortInfo[k].getSortValue = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "sortFn".ToLower())
                                    {
                                        sortInfo[k].sortFn = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    //\"columnId\":\"OPERATE_DT\",\"fieldName\":\"OPERATE_DT\",\"sortOrder\":\"asc\",\"getSortValue\":null,\"sortFn\":null
                                }
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "filterInfo".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        filterInfo = new FilterInfo[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                filterInfo[k] = new FilterInfo();
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    if (arrKey[i].ToLower() == "ColumnId".ToLower())
                                    {
                                        filterInfo[k].ColumnId = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "FieldName".ToLower())
                                    {
                                        filterInfo[k].FieldName = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "Logic".ToLower())
                                    {
                                        filterInfo[k].Logic = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                    if (arrKey[i].ToLower() == "value".ToLower())
                                    {
                                        filterInfo[k].value = GetStringFromValue(JsonData2[arrKey[i]]);
                                    }
                                }
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "remotePaging".ToLower())
                    {
                        remotePaging = GetStringFromValue(sss.Value).ToUpper() == "TRUE";
                    }

                    if (sss.Key.ToLower() == "parameters".ToLower())
                    {
                        parameters = new msBsc.AppLogic.Cfg.SynDictionary(false);
                        Dictionary<string, object> JsonData2 = (Dictionary<string, object>)sss.Value;
                        string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                        //JsonData2.Keys.CopyTo(arrKey, 0);
                        for (int i = 0; i < arrKey.Length; i++)
                        { 
                            parameters[arrKey[i]] = JsonData2[arrKey[i]];
                        }
                    }

                    if (sss.Key.ToLower() == "action".ToLower())
                    {
                        action = GetStringFromValue(sss.Value);
                    }
                    if (sss.Key.ToLower() == "exportType".ToLower())
                    {
                        exportType = GetStringFromValue(sss.Value);
                    }

                    if (sss.Key.ToLower() == "exportFileName".ToLower())
                    {
                        exportFileName = GetStringFromValue(sss.Value);
                    }

                    if (sss.Key.ToLower() == "fieldsName".ToLower())
                    {
                        action = GetStringFromValue(sss.Value);
                    }

                    if (sss.Key.ToLower() == "insertedRecords".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        insertedRecords = new msBsc.AppLogic.Cfg.SynDictionary[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                insertedRecords[k] = new msBsc.AppLogic.Cfg.SynDictionary(false);
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    insertedRecords[k][arrKey[i]] = JsonData2[arrKey[i]];
                                }
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "updatedRecords".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        updatedRecords = new msBsc.AppLogic.Cfg.SynDictionary[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                updatedRecords[k] = new msBsc.AppLogic.Cfg.SynDictionary(false);
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    updatedRecords[k][arrKey[i]] = JsonData2[arrKey[i]];
                                }
                            }
                        }
                    }

                    if (sss.Key.ToLower() == "deletedRecords".ToLower())
                    {
                        System.Object[] ddd = (System.Object[])sss.Value;
                        deletedRecords = new msBsc.AppLogic.Cfg.SynDictionary[ddd.Length];
                        if (ddd.Length > 0)
                        {
                            for (int k = 0; k < ddd.Length; k++)
                            {
                                deletedRecords[k] = new msBsc.AppLogic.Cfg.SynDictionary(false);
                                Dictionary<string, object> JsonData2 = (Dictionary<string, object>)ddd[k];
                                string[] arrKey = JsonData2.Keys.ToArray();// new string[JsonData2.Keys.Count];
                                //JsonData2.Keys.CopyTo(arrKey, 0);
                                for (int i = 0; i < arrKey.Length; i++)
                                { 
                                    deletedRecords[k][arrKey[i]] = JsonData2[arrKey[i]];
                                }
                            }
                        }
                    }
                    //htret[sss.Key] = HttpContext.Current.Server.UrlDecode(GetStringFromValue(sss.Value));

                }
            }
        }
    }



    





    /// <summary>
    /// Summary description for LoadDataInfo
    /// </summary>

    public class PageInfo { 
        public int PageSize; 
        public int PageNum; 
        public int TotalRowNum; 
        public int TotalPageNum; 
        public int StartRowNum; 
        public int EndRowNum;

    }

    public class SortInfo { 
        public string ColumnId; 
        public string FieldName; 
        public string sortOrder;
        public string getSortValue;
        public string sortFn;
    }

    public class FilterInfo { 
        public string ColumnId; 
        public string FieldName; 
        public string Logic; 
        public string value;
    }

    public class ColumnInfo {
         
        public string Id; 
        public string Header; 
        public string FieldName; 
        public string FieldIndex; 
        public string SortOrder; 
        public string Hidden; 
        public string Exportable; 
        public string Printable;
 
    }
     
  

    public class Parameters { 
    
    }

    //[JsonObject(MemberSerialization.OptIn)]
    public class LoadDataInfo
    {
        [JsonProperty("action")]
        public string Action;
        [JsonProperty("recordType")]
        public string recordType;
        [JsonProperty("pageInfo")]
        public PageInfo pageInfo;
        [JsonProperty("sortInfo")]
        public SortInfo[] sortInfoArray;
        [JsonProperty("filterInfo")]
        public FilterInfo[] filterInfoArray;
        [JsonProperty("columnInfo")]
        public ColumnInfo[] columnInfoArray;
        [JsonProperty("parameters")]
        public Parameters parameters;

        public LoadDataInfo()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }

    public class SaveDataInfo<classT>{
        [JsonProperty("action")]
        public string Action;
        [JsonProperty("fieldsName")]
        public string[] fieldsName;

        [JsonProperty("insertedRecords")]
        public classT[] insertedRecords;
        [JsonProperty("updatedRecords")]
        public classT[] updatedRecords;
        [JsonProperty("deletedRecords")]
        public classT[] deletedRecords;

        [JsonProperty("recordType")]
        public string recordType;
        [JsonProperty("parameters")]
        public Parameters parameters;

    }

    //"{\"fieldsName\":[\"QUESTIONS_TYPE_CID\",\"QUESTIONS_NO\",\"QUESTIONS_DESC\",\"DIFFICULTY_TYPE_CIID\",\"ANSWER_HINT_DESC\",\"REFERENCE_DESC\",\"QUESTIONS_TOTAL_MARK\",\"INITIAL_DT\",\"INITIAL_USR\"],\"recordType\":\"object\",\"parameters\":{},\"action\":\"save\",\"insertedRecords\":[{\"QUESTIONS_TYPE_CID\":\"s\",\"QUESTIONS_NO\":\"s\",\"QUESTIONS_DESC\":\"s\",\"DIFFICULTY_TYPE_CIID\":\"s\",\"ANSWER_HINT_DESC\":\"s\",\"REFERENCE_DESC\":\"s\",\"QUESTIONS_TOTAL_MARK\":2,\"INITIAL_DT\":\"2010-07-05\",\"INITIAL_USR\":\"f\"}],\"updatedRecords\":[],\"deletedRecords\":[]}"


}